{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "img_width = 28\n",
    "img_height = 28\n",
    "channels = 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "batch_size = 500\n",
    "num_epochs = 80\n",
    "iterations = 3\n",
    "nb_augmentation = 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "fashion_classes = {0:'T恤',\n",
    "1:'裤子',\n",
    "2:'套衫',\n",
    "3:'裙子',\n",
    "4:'外套',\n",
    "5:'凉鞋',\n",
    "6:'汗衫',\n",
    "7:'运动鞋',\n",
    "8:'包',\n",
    "9:'踝靴',}\n",
    "mnist_classes =[i for i in range (10)]\n",
    "num_classes =10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train Samples 60000\n",
      "Test Samples 10000\n"
     ]
    }
   ],
   "source": [
    "import tensorflow_datasets as tfds\n",
    "train_fasion_mnist=tfds.as_numpy(tfds.load(\"fashion_mnist\",split=\"train\",data_dir=\"./\",download=False,batch_size=-1))\n",
    "X_train,y_train=train_fasion_mnist[\"image\"],train_fasion_mnist[\"label\"]\n",
    "test_fasion_mnist = tfds.as_numpy(tfds.load(\"fashion_mnist\",split=\"test\",data_dir=\"./\",download=False,batch_size=-1))\n",
    "X_test,y_test=test_fasion_mnist[\"image\"],test_fasion_mnist[\"label\"]\n",
    "print(\"Train Samples\",len(X_train))\n",
    "print(\"Test Samples\",len(X_test))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "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3AsvvDA6k27Fphu76TWgyWbs3NxcdNauXbuiuQsXLozOpJu40950Tq5s3bZtW3TWNK/gTTeJp73RnUi/a+l1uMm/hWn+fW5mLvntjKeffjo66/Dhw9GcNyWgiigBVUQJqCJKQBVRAqqIElBFlIAqogRUESWgyky62TkMw7B79+7JoUOHRueOHj06OrN///7oM9PN73RjN92MTaRb2BsbG1P7zGlL/jySe7yHIbv/PJVudJ85cyaaS79H8/PzozPJhv4w5D9D8psB6VnJHePDkG90nzhxYnTm9OnT0Vm333770clkcnBszpsSUEWUgCqiBFQRJaCKKAFVRAmoIkpAFVECqogSUGVTd3QvLCwM77777ujca6+9Njrz2WefRZ+Zbk2nW8eJ9E7q63HetDd2k3u1063pdKs++TtdW1uLzrr55pujucceeyya++GHH0ZnHnjggeis9fX1aC79jifS70f6d7V3797RmfSu+pQ3JaCKKAFVRAmoIkpAFVECqogSUEWUgCqiBFTZ1PLkjh07hnvuuWd07sMPPxydOXbsWPSZb775ZjT3ySefRHOrq6vR3DQly5Pp0lt6res0bd2afU0ef/zxaO6ll14anXnxxRejs+64445oLpVcDzw7OzvVz0yv101cvXo1mkt/hmRxdprPPwzelIAyogRUESWgiigBVUQJqCJKQBVRAqqIElBFlIAqm9ronqaHH344mvvggw+m+rk//vjj6Mw333wTnfX1119HcysrK9FcIr16dHFxMZp75JFHRmeeeOKJ6KxmGxsb0dzCwsLozN133x2dlW7fJxvzW7Zsic7auXNnNJdel7y0tDQ6s2/fvuislDcloIooAVVECagiSkAVUQKqiBJQRZSAKqIEVBEloMpMutk5DMNw8ODByZEjR67h4wD/VjMzM0cnk8nBsTlvSkAVUQKqiBJQRZSAKqIEVBEloIooAVVECagiSkAVUQKqiBJQRZSAKqIEVBEloIooAVVECagiSkAVUQKqiBJQRZSAKqIEVBEloIooAVVECagiSkAVUQKqiBJQRZSAKqIEVBEloIooAVVECagiSkAVUQKqiBJQRZSAKqIEVBEloIooAVVECagiSkAVUQKqiBJQRZSAKqIEVBEloIooAVVECagiSkAVUQKqiBJQRZSAKqIEVBEloIooAVVECagiSkCVmclkkg/PzPzvMAynrt3jAP9i/zOZTPaODW0qSgDXmv99A6qIElBFlIAqogRUESWgiigBVUQJqCJKQBVRAqr8B7oRCBfqzMLlAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 360x360 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "类型: 踝靴\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "plt.figure(figsize=(5,5))\n",
    "i=np.random.randint(len(X_train))\n",
    "img = X_train[i].reshape(28, 28)\n",
    "plt.xticks([])\n",
    "plt.yticks([])\n",
    "plt.grid(False)\n",
    "plt.imshow(img, cmap=plt.cm.binary)\n",
    "plt.show()\n",
    "print(\"类型:\",fashion_classes[y_train[i]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 144,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "import numpy as np\n",
    "def img_scale(image):#放大\n",
    "    result = cv2.resize(image, (600, 600))\n",
    "    #cv2.imshow(\"scale\", result)\n",
    "    #cv2.waitKey(0)\n",
    "    return result\n",
    "def img_rotation(image):\n",
    "    # 原图的高、宽 以及通道数\n",
    "    rows, cols= image.shape\n",
    "\n",
    "    # 绕图像的中心旋转\n",
    "    # 参数：旋转中心 旋转度数 scale\n",
    "    M = cv2.getRotationMatrix2D((cols / 2, rows / 2), 90, 1)\n",
    "    # 参数：原始图像 旋转参数 元素图像宽高\n",
    "    rotated = cv2.warpAffine(image, M, (cols, rows))\n",
    "\n",
    "    # 显示图像\n",
    "    cv2.imshow(\"rotated\", rotated)\n",
    "    cv2.waitKey(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 145,
   "metadata": {},
   "outputs": [],
   "source": [
    "def img_flip(image):\n",
    "    # 0以X轴为对称轴翻转,>0以Y轴为对称轴翻转, <0X轴Y轴翻转\n",
    "    horizontally = cv2.flip(image, 0)  # 水平镜像\n",
    "    vertically = cv2.flip(image, 1)  # 垂直镜像\n",
    "    hv = cv2.flip(image, -1)  # 水平垂直镜像\n",
    "\n",
    "    # 显示图形\n",
    "    cv2.imshow(\"Horizontally\", horizontally)\n",
    "    cv2.imshow(\"Vertically\", vertically)\n",
    "    cv2.imshow(\"Horizontally & Vertically\", hv)\n",
    "    cv2.waitKey(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 146,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1"
      ]
     },
     "execution_count": 146,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import cv2\n",
    "img1=cv2.resize(img,(600,600))#大图为200*200\n",
    "img1_r=600-img1.shape[0]#第0个维度填充到200需要的像素点个数\n",
    "img1_b=600-img1.shape[1]#第1个维度填充到200需要的像素点个数\n",
    "img1_pad=np.pad(img1,((0,img1_r),(0,img1_b)),'constant', constant_values=0)\n",
    "cv2.imshow(\"tianchong\",img1)\n",
    "cv2.waitKey(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 147,
   "metadata": {},
   "outputs": [],
   "source": [
    "t=img_scale(img)\n",
    "img_rotation(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 148,
   "metadata": {},
   "outputs": [],
   "source": [
    "img_flip(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "from tensorflow.keras.preprocessing.image import ImageDataGenerator\n",
    "#定义用于图像增强的函数\n",
    "datagen = ImageDataGenerator(\n",
    "    rotation_range=10,        #旋转角度\n",
    "#    width_shift_range=0.2,   #水平偏移\n",
    "#    height_shift_range=0.2,  #垂直偏移\n",
    "#    shear_range=0.2,         #随机错切变换的角度\n",
    "#    zoom_range=0.2,          #随机缩放的范围\n",
    "    horizontal_flip=True,    #随机将一半图像水平翻转\n",
    "    fill_mode='nearest'      #随机将一半图像水平翻转\n",
    ")\n",
    "\n",
    "#image 原始图像\n",
    "#nb_augmentation 增加的数量\n",
    "#images 初始化后的图像\n",
    "\n",
    "def image_augmentation(image, nb_of_augmentation):\n",
    "    images = []\n",
    "    image = image.reshape(1,img_height,img_width,channels)\n",
    "    i = 0\n",
    "    for x_batch in datagen.flow(image,batch_size=1):\n",
    "        images.append(x_batch)\n",
    "        i += 1\n",
    "        if i >= nb_of_augmentation:\n",
    "            break\n",
    "    return images"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "\n",
    "#targets 目标\n",
    "#use_sugmentation 进行数据增强则设置为True\n",
    "#nb_of_augmentation 图像增强设置的数量\n",
    "\n",
    "def preprocess_data(images, targets, use_augmentation=False, nb_of_augmentation=1):\n",
    "    \n",
    "    X = []\n",
    "    y = []\n",
    "    for x_, y_ in zip(images, targets):\n",
    "        #像素缩放\n",
    "        x_ = x_ / 255.0\n",
    "        #数据增强\n",
    "        if use_augmentation:\n",
    "            argu_img = image_augmentation(x_, nb_of_augmentation)\n",
    "            for a in argu_img:\n",
    "                X.append(a.reshape(img_height, img_width, channels))\n",
    "                y.append(y_)\n",
    "                \n",
    "        X.append(x_)\n",
    "        y.append(y_)\n",
    "    print(\"预处理结束：%i 个样本\\n\" % len(X))\n",
    "    return np.array(X),tf.keras.utils.to_categorical(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "预处理结束：180000 个样本\n",
      "\n",
      "预处理结束：10000 个样本\n",
      "\n"
     ]
    }
   ],
   "source": [
    "X_train_shaped, y_train_shaped = preprocess_data(\n",
    "    X_train, y_train,\n",
    "    use_augmentation = True,\n",
    "    nb_of_augmentation = nb_augmentation\n",
    ")\n",
    "X_test_shaped, y_test_shaped = preprocess_data(X_test,y_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "def create_model():\n",
    "    cnn = tf.keras.Sequential()\n",
    "    cnn.add(tf.keras.layers.InputLayer(input_shape=(img_height,img_width,channels)))\n",
    "    cnn.add(tf.keras.layers.BatchNormalization())\n",
    "    cnn.add(tf.keras.layers.Convolution2D(64,(4,4),padding= 'same',activation='relu'))\n",
    "    cnn.add(tf.keras.layers.MaxPooling2D(pool_size=(2,2)))\n",
    "    cnn.add(tf.keras.layers.Dropout(0.1))\n",
    "    cnn.add(tf.keras.layers.Convolution2D(64,(4,4),activation='relu'))\n",
    "    cnn.add(tf.keras.layers.MaxPooling2D(pool_size=(2,2)))\n",
    "    cnn.add(tf.keras.layers.Dropout(0.3))\n",
    "    cnn.add(tf.keras.layers.Flatten())\n",
    "    cnn.add(tf.keras.layers.Dense(256,activation='relu'))\n",
    "    cnn.add(tf.keras.layers.Dropout(0.5))\n",
    "    cnn.add(tf.keras.layers.Dense(64,activation='relu'))\n",
    "    cnn.add(tf.keras.layers.BatchNormalization())\n",
    "    cnn.add(tf.keras.layers.Dense(num_classes,activation=\"softmax\" ))\n",
    "    cnn.compile(loss= 'categorical_crossentropy',optimizer=tf.keras.optimizers.Adam(),metrics=['accuracy'])\n",
    "    return cnn                                 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model: \"sequential_4\"\n",
      "_________________________________________________________________\n",
      "Layer (type)                 Output Shape              Param #   \n",
      "=================================================================\n",
      "batch_normalization_6 (Batch (None, 28, 28, 1)         4         \n",
      "_________________________________________________________________\n",
      "conv2d_6 (Conv2D)            (None, 28, 28, 64)        1088      \n",
      "_________________________________________________________________\n",
      "max_pooling2d_6 (MaxPooling2 (None, 14, 14, 64)        0         \n",
      "_________________________________________________________________\n",
      "dropout_9 (Dropout)          (None, 14, 14, 64)        0         \n",
      "_________________________________________________________________\n",
      "conv2d_7 (Conv2D)            (None, 11, 11, 64)        65600     \n",
      "_________________________________________________________________\n",
      "max_pooling2d_7 (MaxPooling2 (None, 5, 5, 64)          0         \n",
      "_________________________________________________________________\n",
      "dropout_10 (Dropout)         (None, 5, 5, 64)          0         \n",
      "_________________________________________________________________\n",
      "flatten_3 (Flatten)          (None, 1600)              0         \n",
      "_________________________________________________________________\n",
      "dense_9 (Dense)              (None, 256)               409856    \n",
      "_________________________________________________________________\n",
      "dropout_11 (Dropout)         (None, 256)               0         \n",
      "_________________________________________________________________\n",
      "dense_10 (Dense)             (None, 64)                16448     \n",
      "_________________________________________________________________\n",
      "batch_normalization_7 (Batch (None, 64)                256       \n",
      "_________________________________________________________________\n",
      "dense_11 (Dense)             (None, 10)                650       \n",
      "=================================================================\n",
      "Total params: 493,902\n",
      "Trainable params: 493,772\n",
      "Non-trainable params: 130\n",
      "_________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "create_model().summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "iteration: 0\n",
      "Epoch 1/2\n",
      "288/288 [==============================] - ETA: 0s - loss: 0.5984 - accuracy: 0.7867\n",
      "Epoch 00001: val_loss improved from inf to 0.51948, saving model to fashion_mnist-0.hdf5\n",
      "288/288 [==============================] - 187s 649ms/step - loss: 0.5984 - accuracy: 0.7867 - val_loss: 0.5195 - val_accuracy: 0.8719\n",
      "Epoch 2/2\n",
      "288/288 [==============================] - ETA: 0s - loss: 0.3608 - accuracy: 0.8680\n",
      "Epoch 00002: val_loss improved from 0.51948 to 0.27558, saving model to fashion_mnist-0.hdf5\n",
      "288/288 [==============================] - 182s 632ms/step - loss: 0.3608 - accuracy: 0.8680 - val_loss: 0.2756 - val_accuracy: 0.8968\n",
      "iteration: 1\n",
      "Epoch 1/2\n",
      "288/288 [==============================] - ETA: 0s - loss: 0.5875 - accuracy: 0.7916\n",
      "Epoch 00001: val_loss improved from inf to 0.52949, saving model to fashion_mnist-1.hdf5\n",
      "288/288 [==============================] - 185s 644ms/step - loss: 0.5875 - accuracy: 0.7916 - val_loss: 0.5295 - val_accuracy: 0.8756\n",
      "Epoch 2/2\n",
      "288/288 [==============================] - ETA: 0s - loss: 0.3558 - accuracy: 0.8712\n",
      "Epoch 00002: val_loss improved from 0.52949 to 0.27299, saving model to fashion_mnist-1.hdf5\n",
      "288/288 [==============================] - 183s 635ms/step - loss: 0.3558 - accuracy: 0.8712 - val_loss: 0.2730 - val_accuracy: 0.8994\n",
      "iteration: 2\n",
      "Epoch 1/2\n",
      "288/288 [==============================] - ETA: 0s - loss: 0.5811 - accuracy: 0.7902\n",
      "Epoch 00001: val_loss improved from inf to 0.51771, saving model to fashion_mnist-2.hdf5\n",
      "288/288 [==============================] - 183s 635ms/step - loss: 0.5811 - accuracy: 0.7902 - val_loss: 0.5177 - val_accuracy: 0.8678\n",
      "Epoch 2/2\n",
      "288/288 [==============================] - ETA: 0s - loss: 0.3604 - accuracy: 0.8686\n",
      "Epoch 00002: val_loss improved from 0.51771 to 0.28310, saving model to fashion_mnist-2.hdf5\n",
      "288/288 [==============================] - 184s 638ms/step - loss: 0.3604 - accuracy: 0.8686 - val_loss: 0.2831 - val_accuracy: 0.8939\n"
     ]
    }
   ],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "histories = []\n",
    "for i in range(0,iterations):\n",
    "    print('iteration: %i' % i)\n",
    "    filepath = \"fashion_mnist-%i.hdf5\" % i\n",
    "    X_train_, X_val_, y_train_, y_val_ = train_test_split(X_train_shaped, y_train_shaped,test_size=0.2, random_state=42)\n",
    "    cnn = create_model()\n",
    "    history = cnn.fit(X_train_,y_train_,\n",
    "                      batch_size=batch_size,\n",
    "                      epochs=2,#num_epochs,\n",
    "                      verbose=1,\n",
    "                      validation_data=(X_val_,y_val_),\n",
    "                      callbacks=[\n",
    "                          tf.keras.callbacks.ModelCheckpoint(filepath,monitor= 'val_loss',verbose=1,save_best_only=True)\n",
    "                      ])\n",
    "    histories.append(history.history)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "in user code:\n\n    F:\\anaconda33\\lib\\site-packages\\tensorflow\\python\\keras\\engine\\training.py:1224 test_function  *\n        return step_function(self, iterator)\n    F:\\anaconda33\\lib\\site-packages\\tensorflow\\python\\keras\\engine\\training.py:1215 step_function  **\n        outputs = model.distribute_strategy.run(run_step, args=(data,))\n    F:\\anaconda33\\lib\\site-packages\\tensorflow\\python\\distribute\\distribute_lib.py:1211 run\n        return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)\n    F:\\anaconda33\\lib\\site-packages\\tensorflow\\python\\distribute\\distribute_lib.py:2585 call_for_each_replica\n        return self._call_for_each_replica(fn, args, kwargs)\n    F:\\anaconda33\\lib\\site-packages\\tensorflow\\python\\distribute\\distribute_lib.py:2945 _call_for_each_replica\n        return fn(*args, **kwargs)\n    F:\\anaconda33\\lib\\site-packages\\tensorflow\\python\\keras\\engine\\training.py:1208 run_step  **\n        outputs = model.test_step(data)\n    F:\\anaconda33\\lib\\site-packages\\tensorflow\\python\\keras\\engine\\training.py:1177 test_step\n        y, y_pred, sample_weight, regularization_losses=self.losses)\n    F:\\anaconda33\\lib\\site-packages\\tensorflow\\python\\keras\\engine\\compile_utils.py:204 __call__\n        loss_value = loss_obj(y_t, y_p, sample_weight=sw)\n    F:\\anaconda33\\lib\\site-packages\\tensorflow\\python\\keras\\losses.py:149 __call__\n        losses = ag_call(y_true, y_pred)\n    F:\\anaconda33\\lib\\site-packages\\tensorflow\\python\\keras\\losses.py:253 call  **\n        return ag_fn(y_true, y_pred, **self._fn_kwargs)\n    F:\\anaconda33\\lib\\site-packages\\tensorflow\\python\\util\\dispatch.py:201 wrapper\n        return target(*args, **kwargs)\n    F:\\anaconda33\\lib\\site-packages\\tensorflow\\python\\keras\\losses.py:1535 categorical_crossentropy\n        return K.categorical_crossentropy(y_true, y_pred, from_logits=from_logits)\n    F:\\anaconda33\\lib\\site-packages\\tensorflow\\python\\util\\dispatch.py:201 wrapper\n        return target(*args, **kwargs)\n    F:\\anaconda33\\lib\\site-packages\\tensorflow\\python\\keras\\backend.py:4687 categorical_crossentropy\n        target.shape.assert_is_compatible_with(output.shape)\n    F:\\anaconda33\\lib\\site-packages\\tensorflow\\python\\framework\\tensor_shape.py:1134 assert_is_compatible_with\n        raise ValueError(\"Shapes %s and %s are incompatible\" % (self, other))\n\n    ValueError: Shapes (None, 1) and (None, 10) are incompatible\n",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-35-d1cb101d4e83>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mloss\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0maccuracy\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcreate_model\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mevaluate\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mX_test\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my_test\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'loss: '\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mloss\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'accuracy: '\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0maccuracy\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mF:\\anaconda33\\lib\\site-packages\\tensorflow\\python\\keras\\engine\\training.py\u001b[0m in \u001b[0;36m_method_wrapper\u001b[1;34m(self, *args, **kwargs)\u001b[0m\n\u001b[0;32m    106\u001b[0m   \u001b[1;32mdef\u001b[0m \u001b[0m_method_wrapper\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    107\u001b[0m     \u001b[1;32mif\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_in_multi_worker_mode\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m  \u001b[1;31m# pylint: disable=protected-access\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 108\u001b[1;33m       \u001b[1;32mreturn\u001b[0m \u001b[0mmethod\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    109\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    110\u001b[0m     \u001b[1;31m# Running inside `run_distribute_coordinator` already.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mF:\\anaconda33\\lib\\site-packages\\tensorflow\\python\\keras\\engine\\training.py\u001b[0m in \u001b[0;36mevaluate\u001b[1;34m(self, x, y, batch_size, verbose, sample_weight, steps, callbacks, max_queue_size, workers, use_multiprocessing, return_dict)\u001b[0m\n\u001b[0;32m   1377\u001b[0m             \u001b[1;32mwith\u001b[0m \u001b[0mtrace\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mTrace\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'TraceContext'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mgraph_type\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'test'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mstep_num\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mstep\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1378\u001b[0m               \u001b[0mcallbacks\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mon_test_batch_begin\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mstep\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1379\u001b[1;33m               \u001b[0mtmp_logs\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mtest_function\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0miterator\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1380\u001b[0m               \u001b[1;32mif\u001b[0m \u001b[0mdata_handler\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mshould_sync\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1381\u001b[0m                 \u001b[0mcontext\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0masync_wait\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mF:\\anaconda33\\lib\\site-packages\\tensorflow\\python\\eager\\def_function.py\u001b[0m in \u001b[0;36m__call__\u001b[1;34m(self, *args, **kwds)\u001b[0m\n\u001b[0;32m    778\u001b[0m       \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    779\u001b[0m         \u001b[0mcompiler\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m\"nonXla\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 780\u001b[1;33m         \u001b[0mresult\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_call\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwds\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    781\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    782\u001b[0m       \u001b[0mnew_tracing_count\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_get_tracing_count\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mF:\\anaconda33\\lib\\site-packages\\tensorflow\\python\\eager\\def_function.py\u001b[0m in \u001b[0;36m_call\u001b[1;34m(self, *args, **kwds)\u001b[0m\n\u001b[0;32m    821\u001b[0m       \u001b[1;31m# This is the first call of __call__, so we have to initialize.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    822\u001b[0m       \u001b[0minitializers\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 823\u001b[1;33m       \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_initialize\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mkwds\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0madd_initializers_to\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0minitializers\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    824\u001b[0m     \u001b[1;32mfinally\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    825\u001b[0m       \u001b[1;31m# At this point we know that the initialization is complete (or less\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mF:\\anaconda33\\lib\\site-packages\\tensorflow\\python\\eager\\def_function.py\u001b[0m in \u001b[0;36m_initialize\u001b[1;34m(self, args, kwds, add_initializers_to)\u001b[0m\n\u001b[0;32m    695\u001b[0m     self._concrete_stateful_fn = (\n\u001b[0;32m    696\u001b[0m         self._stateful_fn._get_concrete_function_internal_garbage_collected(  # pylint: disable=protected-access\n\u001b[1;32m--> 697\u001b[1;33m             *args, **kwds))\n\u001b[0m\u001b[0;32m    698\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    699\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0minvalid_creator_scope\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0munused_args\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0munused_kwds\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mF:\\anaconda33\\lib\\site-packages\\tensorflow\\python\\eager\\function.py\u001b[0m in \u001b[0;36m_get_concrete_function_internal_garbage_collected\u001b[1;34m(self, *args, **kwargs)\u001b[0m\n\u001b[0;32m   2853\u001b[0m       \u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mkwargs\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   2854\u001b[0m     \u001b[1;32mwith\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_lock\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 2855\u001b[1;33m       \u001b[0mgraph_function\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0m_\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0m_\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_maybe_define_function\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   2856\u001b[0m     \u001b[1;32mreturn\u001b[0m \u001b[0mgraph_function\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   2857\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mF:\\anaconda33\\lib\\site-packages\\tensorflow\\python\\eager\\function.py\u001b[0m in \u001b[0;36m_maybe_define_function\u001b[1;34m(self, args, kwargs)\u001b[0m\n\u001b[0;32m   3211\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   3212\u001b[0m       \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_function_cache\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmissed\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0madd\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcall_context_key\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 3213\u001b[1;33m       \u001b[0mgraph_function\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_create_graph_function\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   3214\u001b[0m       \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_function_cache\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mprimary\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mcache_key\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mgraph_function\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   3215\u001b[0m       \u001b[1;32mreturn\u001b[0m \u001b[0mgraph_function\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mkwargs\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mF:\\anaconda33\\lib\\site-packages\\tensorflow\\python\\eager\\function.py\u001b[0m in \u001b[0;36m_create_graph_function\u001b[1;34m(self, args, kwargs, override_flat_arg_shapes)\u001b[0m\n\u001b[0;32m   3073\u001b[0m             \u001b[0marg_names\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0marg_names\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   3074\u001b[0m             \u001b[0moverride_flat_arg_shapes\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0moverride_flat_arg_shapes\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 3075\u001b[1;33m             capture_by_value=self._capture_by_value),\n\u001b[0m\u001b[0;32m   3076\u001b[0m         \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_function_attributes\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   3077\u001b[0m         \u001b[0mfunction_spec\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfunction_spec\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mF:\\anaconda33\\lib\\site-packages\\tensorflow\\python\\framework\\func_graph.py\u001b[0m in \u001b[0;36mfunc_graph_from_py_func\u001b[1;34m(name, python_func, args, kwargs, signature, func_graph, autograph, autograph_options, add_control_dependencies, arg_names, op_return_value, collections, capture_by_value, override_flat_arg_shapes)\u001b[0m\n\u001b[0;32m    984\u001b[0m         \u001b[0m_\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0moriginal_func\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mtf_decorator\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0munwrap\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mpython_func\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    985\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 986\u001b[1;33m       \u001b[0mfunc_outputs\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mpython_func\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0mfunc_args\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mfunc_kwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    987\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    988\u001b[0m       \u001b[1;31m# invariant: `func_outputs` contains only Tensors, CompositeTensors,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mF:\\anaconda33\\lib\\site-packages\\tensorflow\\python\\eager\\def_function.py\u001b[0m in \u001b[0;36mwrapped_fn\u001b[1;34m(*args, **kwds)\u001b[0m\n\u001b[0;32m    598\u001b[0m         \u001b[1;31m# __wrapped__ allows AutoGraph to swap in a converted function. We give\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    599\u001b[0m         \u001b[1;31m# the function a weak reference to itself to avoid a reference cycle.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 600\u001b[1;33m         \u001b[1;32mreturn\u001b[0m \u001b[0mweak_wrapped_fn\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m__wrapped__\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwds\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    601\u001b[0m     \u001b[0mweak_wrapped_fn\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mweakref\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mref\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mwrapped_fn\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    602\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mF:\\anaconda33\\lib\\site-packages\\tensorflow\\python\\framework\\func_graph.py\u001b[0m in \u001b[0;36mwrapper\u001b[1;34m(*args, **kwargs)\u001b[0m\n\u001b[0;32m    971\u001b[0m           \u001b[1;32mexcept\u001b[0m \u001b[0mException\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[1;33m:\u001b[0m  \u001b[1;31m# pylint:disable=broad-except\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    972\u001b[0m             \u001b[1;32mif\u001b[0m \u001b[0mhasattr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0me\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"ag_error_metadata\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 973\u001b[1;33m               \u001b[1;32mraise\u001b[0m \u001b[0me\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mag_error_metadata\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mto_exception\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0me\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    974\u001b[0m             \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    975\u001b[0m               \u001b[1;32mraise\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mValueError\u001b[0m: in user code:\n\n    F:\\anaconda33\\lib\\site-packages\\tensorflow\\python\\keras\\engine\\training.py:1224 test_function  *\n        return step_function(self, iterator)\n    F:\\anaconda33\\lib\\site-packages\\tensorflow\\python\\keras\\engine\\training.py:1215 step_function  **\n        outputs = model.distribute_strategy.run(run_step, args=(data,))\n    F:\\anaconda33\\lib\\site-packages\\tensorflow\\python\\distribute\\distribute_lib.py:1211 run\n        return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)\n    F:\\anaconda33\\lib\\site-packages\\tensorflow\\python\\distribute\\distribute_lib.py:2585 call_for_each_replica\n        return self._call_for_each_replica(fn, args, kwargs)\n    F:\\anaconda33\\lib\\site-packages\\tensorflow\\python\\distribute\\distribute_lib.py:2945 _call_for_each_replica\n        return fn(*args, **kwargs)\n    F:\\anaconda33\\lib\\site-packages\\tensorflow\\python\\keras\\engine\\training.py:1208 run_step  **\n        outputs = model.test_step(data)\n    F:\\anaconda33\\lib\\site-packages\\tensorflow\\python\\keras\\engine\\training.py:1177 test_step\n        y, y_pred, sample_weight, regularization_losses=self.losses)\n    F:\\anaconda33\\lib\\site-packages\\tensorflow\\python\\keras\\engine\\compile_utils.py:204 __call__\n        loss_value = loss_obj(y_t, y_p, sample_weight=sw)\n    F:\\anaconda33\\lib\\site-packages\\tensorflow\\python\\keras\\losses.py:149 __call__\n        losses = ag_call(y_true, y_pred)\n    F:\\anaconda33\\lib\\site-packages\\tensorflow\\python\\keras\\losses.py:253 call  **\n        return ag_fn(y_true, y_pred, **self._fn_kwargs)\n    F:\\anaconda33\\lib\\site-packages\\tensorflow\\python\\util\\dispatch.py:201 wrapper\n        return target(*args, **kwargs)\n    F:\\anaconda33\\lib\\site-packages\\tensorflow\\python\\keras\\losses.py:1535 categorical_crossentropy\n        return K.categorical_crossentropy(y_true, y_pred, from_logits=from_logits)\n    F:\\anaconda33\\lib\\site-packages\\tensorflow\\python\\util\\dispatch.py:201 wrapper\n        return target(*args, **kwargs)\n    F:\\anaconda33\\lib\\site-packages\\tensorflow\\python\\keras\\backend.py:4687 categorical_crossentropy\n        target.shape.assert_is_compatible_with(output.shape)\n    F:\\anaconda33\\lib\\site-packages\\tensorflow\\python\\framework\\tensor_shape.py:1134 assert_is_compatible_with\n        raise ValueError(\"Shapes %s and %s are incompatible\" % (self, other))\n\n    ValueError: Shapes (None, 1) and (None, 10) are incompatible\n"
     ]
    }
   ],
   "source": [
    "loss, accuracy = create_model().evaluate(X_test, y_test)\n",
    "print('loss: ', loss)\n",
    "print('accuracy: ', accuracy)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_avg(histories,his_key):\n",
    "    tmp=[]\n",
    "    for history in histories:\n",
    "        tmp.append(history[his_key][np.argmin( history['val_loss'])])\n",
    "    return np.mean(tmp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'sccuracy'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-34-4ded5362ea37>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"训练集:\\t%0.8f loss /%0.8f acc\"\u001b[0m\u001b[1;33m%\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mget_avg\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mhistories\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'loss'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mget_avg\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mhistories\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'sccuracy'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m \u001b[0mprint\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;34m\"验证集:\\t%0.8f loss /%s0.8f acc\"\u001b[0m\u001b[1;33m%\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mget_avg\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mhistories\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'vol_loss'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mget_avg\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mhistories\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'val_accuracy'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m<ipython-input-33-e43b7524ebd4>\u001b[0m in \u001b[0;36mget_avg\u001b[1;34m(histories, his_key)\u001b[0m\n\u001b[0;32m      2\u001b[0m     \u001b[0mtmp\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m     \u001b[1;32mfor\u001b[0m \u001b[0mhistory\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mhistories\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m         \u001b[0mtmp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mhistory\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mhis_key\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0margmin\u001b[0m\u001b[1;33m(\u001b[0m \u001b[0mhistory\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'val_loss'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      5\u001b[0m     \u001b[1;32mreturn\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmean\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtmp\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mKeyError\u001b[0m: 'sccuracy'"
     ]
    }
   ],
   "source": [
    "print(\"训练集:\\t%0.8f loss /%0.8f acc\"%(get_avg(histories,'loss'),get_avg(histories,'sccuracy')))\n",
    "print (\"验证集:\\t%0.8f loss /%s0.8f acc\"%(get_avg(histories,'vol_loss'),get_avg(histories,'val_accuracy')))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
